#自动生成一个正确的身份证号码
import random
import time


def address_id():
    # 取出6位地址码,这里我的信息不全，但需求只是生成一些正确的代码，有兴趣的可以找完整信息
    address_list = [110000, 110100, 110101, 130101, 130121, 130128, 130127, 140401,
                    140411, 140425, 152130, 152202, 152224, 210106, 210122, 320882,
                    320111, 320121, 320106, 320830, 320882]
    address_id = random.choice(address_list)
    return address_id


# 生成年份
def year_id():
    year_id = time.strftime('%Y')
    # 身份证好像是1948年才有
    year_int = random.randint(1948, int(year_id)-10)
    return year_int


def month_id():
    month_id = random.randint(1, 12)
    if month_id < 10:
        return '0' + str(month_id)
    else:
        return month_id


def day_id():
    # 同上，只是生成一些身份证，不考虑所有情况
    day_id = random.randint(1, 28)
    if day_id < 10:
        return '0' + str(day_id)
    else:
        return day_id


def sex_id():
    sex_id = random.randint(1, 999)
    if sex_id < 10:
        return '00' + str(sex_id)
    elif 10 <= sex_id < 100:
        return "0" + str(sex_id)
    else:
        return sex_id

def last_id():
    sum = 0
    # 后面要用，所以变成全局变量
    global number
    number = str(address_id()) + str(year_id()) + str(month_id()) + str(day_id()) + str(sex_id())

    number_list = list(number)
    list2 = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    for i in range(0, 17):
        sum += int(number_list[i]) * list2[i]
    #同上
    global last
    last_int = sum % 11
    #身份证最后一位是前17位，分别乘以参数（list2中），然后相加，再除以11取余数，然后一一相对应。余数0对应1如下
    checkcode = {'0': '1', '1': '0', '2': 'X', '3': '9', '4': '8', '5': '7', '6': '6', '7': '5', '8': '5', '9': '3',
                 '10': '2'}  # 校验码映射
    last = checkcode[str(last_int)]


def main():
    # address_id()
    # year_id()
    # month_id()
    # day_id()
    # sex_id()
    last_id()

    card_id = number+ last
    print(card_id)



if __name__ == '__main__':
    main()
